import { Component, OnInit, ViewChild } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { STColumn, STComponent, STChange, STPage } from '@delon/abc/table';
import {
  SFSchema,
  SFTreeSelectWidgetSchema,
  SFSchemaEnum,
  SFComponent,
  SFRadioWidgetSchema,
} from '@delon/form';
import { GridComponent, PopdetailComponent, HttpService, SupDic } from 'lbf';
import { ActivatedRoute } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd';
// tslint:disable-next-line: no-duplicate-imports
import { Router } from '@angular/router';
import { THIS_EXPR } from '@angular/compiler/src/output/output_ast';
import { of, config } from 'rxjs';
import { delay } from 'rxjs/operators';
// tslint:disable-next-line: use-path-mapping
import { BussFormComponent } from '@shared/components/buss_form/buss_form.component';
import { LbZskcxComponent } from '@shared/components/lb_zskcx/lb_zskcx.component';
import { ZskService } from '../zskservice/zsk.service';
import { ReuseTabService } from '@delon/abc';
import { SafeResourceUrl, DomSanitizer } from '@angular/platform-browser';
import { RepserviceService } from '@core/lb/repservice.service';

@Component({
  selector: 'app-tyjb-sxcl',
  templateUrl: './sxcl.component.html',
  styleUrls: ['./sxcl.component.less'],
})
export class TyjbSxclComponent implements OnInit {
  formdata: {};
  yjsformdata: {};
  bfform: {};

  constructor(
    private route: ActivatedRoute,
    private http: _HttpClient,
    private modal: ModalHelper,
    private lbservice: HttpService,
    public msgSrv: NzMessageService,
    private reuseTabSrv: ReuseTabService,
    private router: Router,
    private supDic: SupDic,
    private zskService: ZskService,
    private sanitizer: DomSanitizer,
    private repservice: RepserviceService,
  ) {
    this.reuseTabSrv.title = '收件';
  }

  sxData={
    sbcl:[],
  };

  dataset = [];
  sbxxdata = [];
  yjsdata = [];
  dad017 = 0;
  dad031 = '';
  dad032 = '';
  AAC002 = '';
  dad075
  dad028;
  dae001;
  isVisible = false;
  isAddVisible = false;
  isYjsVisible = false;
  isDwVisible = false;
  isDwAddVisible = false;
  isDjVisible = false;
  jyshow    = '';
  printshow = '';
  ympage = '';
  biaoti = '';
  show = '1';
  sbxxshow = '';
  clsmshow = '';
  clscshow = '';
  DAD069 = null;
  DAD011;
  bt_mc;
  sccl;
  paras :any;
  errparas :any;
  Sname :FileSname = {save:'DASC_ADD',query:'DZCL_QUERY',del:''}
  @ViewChild('st', { static: false }) st: GridComponent;
  @ViewChild('st1', { static: false }) st1: GridComponent;
  @ViewChild('bf', { static: false }) bf: BussFormComponent;
  @ViewChild('sf', { static: false }) sf: SFComponent;
  @ViewChild('sf1', { static: false }) sf1: SFComponent;
  @ViewChild('sf2', { static: false }) sf2: SFComponent;
  @ViewChild('sbxx', { static: false }) sbxx: SFComponent;
  @ViewChild('yjs', { static: false }) yjs: SFComponent;
  @ViewChild('errst', { static: true }) errst: GridComponent;

  params = { sname: 'JBXX_QUERY', queryparas: { DAD032: this.dad032 } };

  errcolumns:STColumn[] = [
    { title: '事项名称', index: 'DAD031'},
    { title: '校验结果', index: 'ERRMSG'},
  ];
  
  columns: STColumn[] = [
    { title: '', index: 'RN', width: '20px' },
    { title: '个人编号', index: 'AAC001', ui: { hidden: true } },
    { title: '姓名', index: 'AAC003', width: '50px' },
    { title: '社会保障号', index: 'AAC002', width: '100px' },
    { title: '性别', index: 'AAC004', width: '50px', dic: 'AAC004'},
    { title: '出生日期', index: 'AAC006', width: '50px' },
    { title: '户口性质', index: 'AAC009', width: '100px' , dic: 'AAC009'},
    { title: '手机号码', index: 'EAE033', width: '100px' },
    { title: '户籍地', index: 'AAC010', width: '50px' },
    { title: '居住地', index: 'AAE006', width: '80px' },
    {
      title: '操作区',
      width: '50px',
      buttons: [
        {
          text: '办理',
          click: (record: any) => this.tysjsl(record),
        },
      ],
    },
  ];

  dwcolumns: STColumn[] = [
    { title: '', index: 'RN', width: '20px' },
    { title: '单位名称', index: 'AAB004', width: '50px' },
    { title: '统一信用代码', index: 'AAB045', width: '100px' },
    { title: '单位联系人', index: 'AAB016', width: '50px' },
    { title: '单位联系人电话', index: 'AAB018', width: '50px' },
    { title: '单位地址', index: 'AAE006', width: '100px' },
    {
      title: '操作区',
      width: '50px',
      buttons: [
        {
          text: '办理',
          click: (record: any) => this.tysjsl(record),
        },
      ],
    },
  ];

  jbxxSchema: SFSchema = {
    properties: {
      AAC002: { type: 'string', title: '社会保障号' },
      AAC003: { type: 'string', title: '姓名' },
      DAD032: { type: 'string', title: '对象', ui: { hidden: true } },
    },
  };

  dwjbxxSchema: SFSchema = {
    properties: {
      AAB045: { type: 'string', title: '统一信用代码' },
      AAB004: { type: 'string', title: '单位名称' },
      DAD032: { type: 'string', title: '对象', ui: { hidden: true } },
    },
  };

  xzxxSchema: SFSchema = {
    properties: {
      AAC022: {
        type: 'string',
        title: '证件类型',
        enum: this.supDic.getSFDic('DAD074'),
        ui: {
          widget: 'select',
        },
        default: '0',
      },
      AAC002: { type: 'string', title: '社会保障号' },
      AAC003: { type: 'string', title: '姓名' },
      AAC006: { type: 'string', title: '出生日期', format: 'date' },
      AAC004: {
        type: 'string',
        title: '性别',
        enum: this.supDic.getSFDic('AAC004'),
        ui: {
          widget: 'select',
        },
      },
      AAC005: {
        type: 'string',
        title: '民族',
        enum: this.supDic.getSFDic('AAC005'),
        ui: {
          widget: 'select',
        },
      },
      AAC009: {
        type: 'string',
        title: '户籍性质',
        enum: this.supDic.getSFDic('AAC009'),
        ui: {
          widget: 'select',
        },
      },
      AAC010: { type: 'string', title: '户籍地址', ui: 'cascader' },
      EAE033: { type: 'string', title: '手机号码' },
    },
    ui: {
      spanLabelFixed: 100,
      grid: {
        span: 12,
      },
    },
    required: ['AAC022', 'AAC002', 'AAC003', 'AAC004', 'AAC006'],
  };

  dwxzSchema: SFSchema = {
    properties: {
      AAB045: { type: 'string', title: '统一信用代码' },
      AAB004: { type: 'string', title: '单位名称' },
      AAB016: { type: 'string', title: '单位联系人' },
      AAB018: { type: 'string', title: '单位联系人电话' },
      AAE006: { type: 'string', title: '单位地址' },
    },
  };

  sbxxSchema: SFSchema = {
    properties: {
      DAD071: {
        type: 'string',
        title: '',
        // tslint:disable-next-line: no-object-literal-type-assertion
        ui: {
          widget: 'radio',
          asyncData: () =>
            of([
              { label: '当事人', value: '1' },
              { label: '代办人', value: '0' },
            ]).pipe(delay(100)),
          change: console.log,
          grid: {
            span: 24,
          },
        } as SFRadioWidgetSchema,
        default: '1',
      },
      DAD074: {
        type: 'string',
        title: '证件类型',
        enum: this.supDic.getSFDic('DAD074'),
        ui: {
          widget: 'select',
        },
        default: '0',
      },
      DAD070: { type: 'string', title: '申报人证件号码' },
      DAD075: { type: 'string', title: '申报人名称' },

      DAD116: {
        type: 'string',
        title: '证件类型',
        enum: this.supDic.getSFDic('DAD074'),
        ui: {
          widget: 'select',visibleIf: { DAD071: ['0'] }
        },
        default: '0',
      },
      DAD114: { type: 'string', title: '申请人证件号码',ui: { visibleIf: { DAD071: ['0'] } } },
      DAD113: { type: 'string', title: '申请人名称' ,ui: { visibleIf: { DAD071: ['0'] } }},
      DAD115: { type: 'string', title: '联系电话' },

      DAD065: {
        type: 'string',
        title: '',
        enum: [
          { label: '自取', value: '1' },
          { label: '邮寄', value: '2' },
          { label: '短信', value: '3' },
          { label: '无需送达', value: '9' },
        ],
        // tslint:disable-next-line: no-object-literal-type-assertion
        ui: {
          widget: 'radio',
          // asyncData: () => of().pipe(delay(100)),
          change: console.log,
          grid: {
            span: 24,
          },
        } as SFRadioWidgetSchema,
        default: '1',
      },
      DAD127: {
        type: 'string',
        title: '送达联系人',
        ui: { visibleIf: { DAD065: ['2'] } },
      },
      DAD132: {
        type: 'string',
        title: '送达联系人电话',
        ui: { visibleIf: { DAD065: ['2'] } },
      },
      DAD119: {
        type: 'string',
        title: '送达地址',
        ui: { visibleIf: { DAD065: ['2'] } },
      },
    },
    ui: {
      spanLabelFixed: 100,
      grid: {
        span: 8,
      },
    },
    required: [
      'DAD074',
      'DAD070',
      'DAD075',
      'DAD114',
      'DAD113',
      'DAD115',
      'DAD116',
    ],
  };

  // 材料扫描
  clsmcolumns: STColumn[] = [
    { title: '序号', index: 'RN', width: '10%' },
    { title: '材料名称', index: 'DAD029', width: '50%' },
    { title: '历史材料数', index: 'CZE010', width: '15%' },
    { title: '收件未扫描', index: 'CZE012', type: 'checkbox', width: '5%' },
    { title: '当前材料数', index: 'CZE011', width: '10%' },

  ];

  // 子事项选择
  yjscolumns: STColumn[] = [
    { title: '序号', index: 'RN', width: '20px' },
    { title: '子事项名称', index: 'DAD031', width: '50px' },
    { title: '选择', index: 'XZ', width: '50px', type: 'checkbox' },
    { title: '子事项编码', index: 'DAE001', width: '50px' },
    { title: '主事项编码', index: 'DAD017', width: '50px' },
  ];

  yjsSchema: SFSchema = {
    properties: {
      DAE001: { type: 'string', title: '子事项名称' },
    },
  };

  page: STPage = {
    show: false,
  };

  /**********************************知识库展示***************************************** */
  isShowZsk = false;
  temp = false;
  @ViewChild('zskcx', { static: false }) zskcx: LbZskcxComponent;

  /***************************************************************************************************** */
  /********************************************方法区****************************************************/
  /***************************************************************************************************** */
  getSx() {
    this.lbservice
      .lbservice('YJS_QUERY', { para: { DAD017: this.dad017 } })
      .then(res => {
        this.yjsSchema = {
          properties: {
            DAD017: {
              type: 'string',
              title: '事项主键',
              ui: {
                hidden: true,
              },
            },
            DAE001: {
              type: 'string',
              title: '事项选择',
              enum: res.message.list,
              ui: { widget: 'checkbox', span: 24 },
            },
          },
          required: ['DAE001'],
          ui: {
            spanLabelFixed: 80,
            grid: {
              gutter: 20,
              span: 24,
            },
          },
        };
      });
    // return this.sfs;
  }

  ifrmeee: SafeResourceUrl;

  ngOnInit() {
    this.route.queryParams.subscribe(param => {
      // tslint:disable-next-line: no-string-literal
      // tslint:disable-next-line: radix
      this.dad017 = parseInt(param['dad017']);
      // this.sxDAD011= parseInt(param['sxdad011']);
      this.sxDAD011 = this.zskService.getDAD011(this.dad017);
      this.DAD011 = this.sxDAD011;
      this.dataVisible = true;
      
      if (this.sxDAD011 !== NaN && this.sxDAD011 !== 1) {
        if (this.temp && this.zskcx !== undefined) {
          this.zskcx.refresh(this.dad017);
          this.zskcx.ShowLeft = true;
          
        }
        this.temp = true;
      }
      // tslint:disable-next-line: no-string-literal
      this.dad031 = param['dad031'];
      // tslint:disable-next-line: no-string-literal
      this.dad032 = param['dad032'];
      this.params = {
        sname: 'JBXX_QUERY',
        queryparas: { DAD032: this.dad032 },
      };
      // tslint:disable-next-line: no-string-literal
      this.ympage = param['ympage'];
      if (this.ympage === '1') {
        this.btmc(this.ympage);
        //this.sxquery();
      }
    });

    
  }

  ngAfterViewInit() {
    this.zskcx.ShowLeft = true;
  }

  // 基本信息查询
  jbxxcx() {
    if (this.dad032 === '2') {
      this.sf1.setValue('/DAD032', this.dad032);
      this.st.reload(this.sf1.value);
    } else {
      this.sf2.setValue('/DAD032', this.dad032);
      this.st1.reload(this.sf2.value);
    }
  }
  // 基本信息新增按钮
  xzjbxx() {
    if (this.dad032 === '2') {
      this.isAddVisible = true;
    } else {
      this.isDwAddVisible = true;
    }
  }
  // tslint:disable-next-line: member-ordering

  btmc(pagenum) {
    if (pagenum === '1') {
      this.bt_mc = '<材料初审>' + this.dad031;
    } else if (pagenum === '2') {
      this.bt_mc = '<申报信息确认>' + this.dad031;
    } else if (pagenum === '3') {
      this.bt_mc = '<材料扫描>' + this.dad031;
    }
  }

  add() {
    // this.modal
    //   .createStatic(FormEditComponent, { i: { id: 0 } })
    //   .subscribe(() => this.st.reload());
  }
  /*
  sxquery(){
    this.lbservice.lbservice("SXCL_QUERY",{para:{DAD017:this.dad017}}).then(resdata=>{
      if (resdata.code < 1) {
        this.msgSrv.error(resdata.errmsg);
      } else {
      this.dataset=resdata.message.list;
      this.DAD011 = this.dataset[0].DAD011;
      }
    });
    
  }*/

  handleCancel(id) {
    if (id === 'cx') {
      this.isVisible = false;
    } else if (id === 'xz') {
      this.isAddVisible = false;
    } else if (id === 'yjs') {
      this.isYjsVisible = false;
    } else if (id === 'dwcx') {
      this.isDwVisible = false;
    } else if (id === 'dwxz') {
      this.isDwAddVisible = false;
    }
  }
  handleOkAdd() {
    this.isVisible = false;

    this.lbservice.lbservice('FR_XXADD', {}).then(resdata => {
      if (resdata.code < 1) {
        this.msgSrv.error(resdata.errmsg);
      } else {
        this.msgSrv.info('新增成功');
        // this.st.st.reload();
        // this.reload();
      }
    });
  }

  ryquery() {
    if (this.DAD011 === '1') {
      this.isYjsVisible = true;
      this.getSx();
    } else {
      if (this.dad032 === '2') {
        this.isVisible = true;
      } else {
        this.isDwVisible = true;
      }

      this.lbservice
        .lbservice('SJXX_ADD', {
          para: {
            DAD011: this.DAD011,
            DAD017: this.dad017,
            DAD069: this.DAD069,
          },
        })
        .then(resdata => {
          if (resdata.code < 1) {
            this.msgSrv.error(resdata.errmsg);
          } else {
            this.yjsdata = resdata.message.list;
            this.DAD069 = this.yjsdata[0].DAD069;
            // console.log(this.DAD069);
          }
        });
    }
  }

  yjs_add() {
    this.isYjsVisible = false;
    this.isVisible = true;
    this.yjsformdata = this.yjs.value;
    this.lbservice
      .lbservice('SJXX_ADD', {
        para: {
          DAD011: this.DAD011,
          AD17: this.yjs.value,
          DAD069: this.DAD069,
          DAD017: this.dad017,
        },
      })
      .then(resdata => {
        if (resdata.code < 1) {
          this.msgSrv.error(resdata.errmsg);
        } else {
          const data = resdata.message.list;
          this.DAD069 = data[0].DAD069;
        }
      });
  }
  // 办理按钮
  tysjsl(id) {
    this.isVisible = false;
    this.AAC002 = id.AAC002;
    this.sbxxqr();
    this.btmc('2');
  }

  sbxxqr() {
    this.sbxxshow = '1';
    this.show = '';
    this.clsmshow = '';
    this.lbservice
      .lbservice('SBJCXX_QUERY', {
        para: { AAC002: this.AAC002, DAD017: this.dad017, DAD069: this.DAD069 },
      })
      .then(resdata => {
        this.dataset = resdata.message.list;
        this.sbxx.setValue('/DAD070', this.dataset[0].DAD070);
        this.sbxx.setValue('/DAD075', this.dataset[0].DAD075);
        this.sbxx.setValue('/DAD114', this.dataset[0].DAD114);
        this.sbxx.setValue('/DAD113', this.dataset[0].DAD113);
        this.sbxx.setValue('/DAD115', this.dataset[0].DAD115);
        this.sbxx.setValue('/DAD119', this.dataset[0].DAD119);
        this.sbxx.setValue('/DAD127', this.dataset[0].DAD127);
        this.sbxx.setValue('/DAD132', this.dataset[0].DAD132);
        this.dad075 = this.dataset[0].DAD075;
        
        this.bf.DAD069 = this.DAD069

      });
  }

  returnone() {
    this.sbxxshow = '';
    this.show = '1';
    this.clsmshow = '';
    this.btmc('1');
    this.zskcx.refresh(this.dad017);
    this.zskcx.ShowLeft = true;
   
    // this.sxquery();
  }

 

  nextone() {
    
    if (!this.sbxx.valid) {
      this.msgSrv.error('必输项不能为空');
      return;
    }
    if (!this.bf.vaild) {
      this.msgSrv.error('业务表单必输项不能为空');
      return;
    }
    this.formdata = this.sbxx.value;
    this.bfform = this.bf.value;
    this.lbservice
      .lbservice('SBJCXX_ADD', {
        para: {
          AD08: this.sbxx.value,
          DAD017: this.dad017,
          DAD069: this.DAD069,
          JK02: this.bf.value,
        },
      })
      .then(resdata => {
        if (resdata.code < 1) {
          this.msgSrv.error(resdata.errmsg);
          if (resdata.errmsg.indexOf("校验错误") >= 0){
            this.queryjk04();
          }
        } else {
          const data = resdata.message.list;
          this.DAD069 = data[0].DAD069;
          this.sbxxshow = '';
          this.show = '';
          this.clsmshow = '1';
          this.btmc('3');
          this.sccl = '请选择需要上传的材料';
          // tslint:disable-next-line: no-shadowed-variable
          this.lbservice
            .lbservice('CLSM_QUERY', {
              para: { DAD069: this.DAD069, DAD017: this.dad017,FLAG:'1'  },
            })
            .then(resdata => {
              if (resdata.code === 0) {
                this.msgSrv.error(resdata.errmsg);
              } else {
                this.dataset = resdata.message.list;
                this.initSxDataSbcl();
              }
            });
        }
      });
  }

  clcx(){
    const resList=[];;
    this.sxData.sbcl.forEach(fi=>{
      fi.sbclCheckGroup.forEach(item=>{
        if(item.checked){
          resList.push(item.value);
        }
      });
    })
    
    // if(this.clxxVisible===false){
    //   this.clxxParam={DAD017:this.DAD017,CKLIST:resList};
    // }else{
    //   this.clxxST.reload({DAD017:this.DAD017,CKLIST:resList});
    // }
    this.lbservice.lbservice("SXCL_JSXX",{para:{DAD069:this.DAD069,CKLIST:resList}}).then(resdata=>{
      console.log(resdata.message.list);
      if (resdata.code === 0) {
        this.msgSrv.error(resdata.errmsg);
      } else {
        this.dataset = resdata.message.list;
        // this.initSxDataSbcl();
      }
    })
  }

  clcz(){
    this.sxData.sbcl.forEach(fi=>{
      fi.sbclCheckGroup.forEach(item=>{
        if(item.checked){
          item.checked=false;
        }
      });
    })
  }

  delete() {
    this.isDjVisible = true;
    console.log("DAD069==="+this.DAD069);
    console.log("DAD017==="+this.dad017);
    // this.lbservice
    //   .lbservice('SBXX_DEL', { para: { DAD069: this.DAD069 } })
    //   .then(resdata => {
    //     if (resdata.code === 0) {
    //       this.msgSrv.error(resdata.errmsg);
    //     }
    //   });
  }

  returntwo() {
    this.btmc('2');
    this.sbxxshow = '1';
    this.show = '';
    this.clsmshow = '';
  }
  nexttwo() { 
    this.lbservice
      .lbservice('SJJS_ADD', { para: { DAD069: this.DAD069 } })
      .then(resdata => {
        if (resdata.code === 0) {
          this.msgSrv.error(resdata.errmsg);
        }
      });

    this.printshow = '1';
    this.clsmshow = '';
    this.print();
  }

  addDone(event) {
    console.log(event);
  }
  _click(e: STChange) {
    if (!e.click) {
      this.sccl = '请选择需要上传的材料条目';
      this.dad028 = null;
      return;
    }
    this.sccl = '请上传 ' + e.click.item.SXMC;
    this.dad028 = e.click.item.DAD028;
    this.dae001 = e.click.item.DAE001;
    this.paras = {DAD069:this.DAD069,DAD028:this.dad028,DAE001:this.dae001};
    
  }


  print(){
    // const repid = '21';
    // const inpara = { DAD069: this.DAD069 };
    this.repservice.getRep('21', { dad069: this.DAD069 }).then(resdata => {
      if (resdata != null) {
        this.ifrmeee = this.sanitizer.bypassSecurityTrustResourceUrl(
          String(resdata),
        );
      } else {
        alert('不存在报表');
      }
    });
    
  }

  queryjk04(){
    this.errparas = {DAD069:this.DAD069};
    console.log(this.errparas);
    this.jyshow = '1';
    
    this.errst.reload(this.errparas);

  }

  handleDjCancel(){
    this.isDjVisible = false;
  }

  /********************************************页面展示***************************************** */
  zsxList = [];
  sxDAD011;
  dataVisible = false;
  initZsxData() {
    this.dataVisible = false;
    this.lbservice
      .lbservice('ZSKWH_QSXZSX', { para: { DAD017: this.dad017 } })
      .then(resdata => {
        if (resdata.code > 0) {
          this.zsxList = resdata.message.list;
        } else {
          console.log(resdata.errmsg);
        }
      });
  }

  zskcxTcVisible = false;
  zskcxTcDad017;
  showZskcxTc(para: any) {
    this.zskcxTcDad017 = para;
    this.zskcxTcVisible = true;
  }
  zskcxTcCancel() {
    this.zskcxTcVisible = false;
    this.zskcxTcDad017 = undefined;
  }

  initSxDataSbcl(){
    // this.lbs.lbservice("CLTJWH_QCLTJ",{para:{DAD017:this.DAD017}}).then(resdata=>{
    //   if(resdata.code>0){
    //     this.sxData.sbcl=resdata.message.list;
        
    //   }else{
    //     console.log(resdata.errmsg);
    //   }
    // });
    this.lbservice.lbservice("ZSKCX_QSBCL",{para:{DAD017:this.dad017}}).then(resdata=>{
      if(resdata.code>0){
        this.sxData.sbcl=resdata.message.list;
        this.sxData.sbcl.forEach(item=>{
          const sbclCheckGroup=[];
          item.BQ_LIST.forEach(bq_item=>{
            const check_item={ label: '', value: '' }
            check_item.label=bq_item.DAD100;
            check_item.value=bq_item.DAD099;
            sbclCheckGroup.push(check_item);
          });
          item['sbclCheckGroup']=sbclCheckGroup;
        });
        console.log(this.sxData.sbcl);
      }else{
        console.log(resdata.errmsg);
      }
    });
  }


  
}


interface FileSname {
  /**
   * 文件上传
   */
  save?: string;
  /**
   * 文件预览
   */
  query: string;
  /**
   * 文件删除
   */
  del?: string;
}